/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.list.search;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import twaver.Element;
import twaver.Generator;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.VisibleFilter;
import twaver.list.TList;
import twaver.Node;
import demo.DemoNode;
import demo.DemoPane;
import demo.DemoUtil;
import demo.MainPane;

public class QuickSearchDemo extends DemoPane{

	private JTextField search = DemoUtil.createColorTextField();
	private TDataBox box = new TDataBox();
	private TList list = new TList(box);
	
	public QuickSearchDemo(){
		this.add(search, BorderLayout.NORTH);
		this.add(new JScrollPane(list), BorderLayout.CENTER);
		
		// init box content
		Enumeration e = MainPane.box.depthFirstEnumeration();
		while(e.hasMoreElements()){
			Element element = (Element)e.nextElement();
    		if(element instanceof DemoNode){
    			Node node = new Node(element.getID());
    			node.setName(element.getName());
    			box.addElement(node);
    		}
    	}
		
		search.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e) {
				list.publishData();
			}
		});
		
		// add visible filter
		list.addVisibleFilter(new VisibleFilter(){
			public boolean isVisible(Element element) {
		        String text = search.getText();
		        if (text == null || text.trim().equals("")) {
		            return true;
		        } 
		        return element.getName().toLowerCase().indexOf(text.toLowerCase()) >= 0;
			}
		});
		
		// set label generator
		list.setElementLabelGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				DemoNode node = (DemoNode)MainPane.box.getElementByID(element.getID());
				DemoPane demo = node.getDemoPane();
				String name = node.getName();
				String className = node.getDemoClass().getName();
				String desc = null;
				if(demo == null){
					desc = " <font color=green>" + "UNLOADED!" + "</font>";
				}else{
					desc = " <font color=red>" + "loaded at " + demo.getCreateTime() + "</font>";
				}
				return "<html><font color=blue>" + name + "</font><br>" + 
						"<em>" + className + desc + "</em></html>";
			}
		});
		
		// set icon generator
		String url = "/demo/resource/images/earth.png";
		final ImageIcon disableIcon = TWaverUtil.createDisabledImage(url);
		final ImageIcon enableIcon = TWaverUtil.getImageIcon(url);
		list.setElementIconGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				DemoNode node = (DemoNode)MainPane.box.getElementByID(element.getID());
				DemoPane demo = node.getDemoPane();
				if(demo == null){
		            return disableIcon;
				}else{
					return enableIcon;
				}
			}
		});
		
        // add double click action listener
        list.addElementDoubleClickedActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
                Element element = (Element)e.getSource();
                DemoNode node = (DemoNode)MainPane.box.getElementByID(element.getID());
                MainPane.box.getSelectionModel().setSelection(node);
			}
        });
	}
	

}